package com.changgou.order.service.impl;

import com.changgou.order.dao.TaskHisMapper;
import com.changgou.order.dao.TaskMapper;
import com.changgou.order.pojo.Task;
import com.changgou.order.pojo.TaskHis;
import com.changgou.order.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author kfzhao
 * @version 1.0
 * @className TaskServiceImpl
 * @description
 * @date 2022/1/4 1:04
 */
@Service
@Slf4j
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskHisMapper taskHisMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delTask(Task task) {
        // 1.记录删除时间
        task.setDeleteTime(new Date());
        Long taskId = task.getId();
        task.setId(null);

        // bean拷贝
        TaskHis taskHis = new TaskHis();
        // 方法调用的前提：所有属性名称必须一致
        BeanUtils.copyProperties(task, taskHis);

        // 记录历史任务数据
        taskHisMapper.insert(taskHis);
        // 删除原有任务数据
        task.setId(taskId);
        taskMapper.deleteByPrimaryKey(task);
        log.info("当前的订单服务完成了添加历史任务，并删除原有任务的操作");
    }
}
